package com.scoy.honeymei.adapter.tabadapter;

import android.content.Context;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName: NormalAdapter$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateRemark: 更新内容
 */
public abstract class OyAdapter<T> extends RecyclerView.Adapter {
    public Context context;
    public List<T> datalist;
    private int maxNum = -1;

    public OyAdapter(Context context) {
        this.context = context;
        this.datalist = new ArrayList<T>();
    }

    @Override
    public int getItemCount() {
        if (maxNum == -1) {
            return datalist == null ? 0 : datalist.size();
        } else {
            return datalist == null ? 0 : Math.min(datalist.size(), maxNum);
        }
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    public OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }

    public interface OnTwoClick {
        void twoClick(int pos);
    }

    public OnTwoClick onTwoClick;

    public void setOnTwoClick(OnTwoClick onTwoClick) {
        this.onTwoClick = onTwoClick;
    }

    public interface OnThreeClick {
        void threeClick(int pos);
    }

    public OnThreeClick onThreeClick;

    public void setOnThreeClick(OnThreeClick onThreeClick) {
        this.onThreeClick = onThreeClick;
    }

    public OnFourClick onFourClick;

    public void setOnFourClick(OnFourClick onFourClick) {
        this.onFourClick = onFourClick;
    }

    public interface OnFourClick {
        void fourClick(int pos);
    }

    public OnFiveClick onFiveClick;

    public void setOnFiveClick(OnFiveClick onFiveClick) {
        this.onFiveClick = onFiveClick;
    }

    public interface OnFiveClick {
        void fiveClick(int pos);
    }

    public void setNewData(@Nullable List<T> datalist) {
        this.datalist.clear();
        this.datalist = datalist;
        notifyDataSetChanged();
    }

    /*    public void setNewData2(@Nullable List<T> datalist) {
            this.datalist.clear();
            this.datalist.addAll(datalist);
            notifyItemRangeInserted( this.datalist.size() - datalist.size(), datalist.size());
            compatibilityDataSizeChanged(datalist.size());

        }*/
    public void addData(@NonNull Collection<? extends T> newData) {
        this.datalist.addAll(newData);
        notifyItemRangeInserted(datalist.size() - newData.size(), newData.size());
        compatibilityDataSizeChanged(newData.size());
    }

    /**
     * compatible getLoadMoreViewCount and getEmptyViewCount may change
     *
     * @param size Need compatible data size
     */
    private void compatibilityDataSizeChanged(int size) {
        final int dataSize = datalist == null ? 0 : datalist.size();
        if (dataSize == size) {
            notifyDataSetChanged();
        }
    }

    /**
     * Get the data of list
     *
     * @return 列表数据
     */
    @NonNull
    public List<T> getData() {
        if (maxNum != -1) {
            List<T> datalist2 = new ArrayList<T>();
            for (int i = 0; i < Math.min(datalist.size(), maxNum); i++) {
                datalist2.add(datalist.get(i));
            }
            return datalist2;
        } else {
            return datalist;
        }
    }


    public void clearData() {
        this.datalist.clear();
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Nullable
    public T getItem(@IntRange(from = 0) int position) {
        if (position < datalist.size())
            return datalist.get(position);
        else
            return null;
    }


    public void deletItem(@IntRange(from = 0) int position) {
        datalist.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, datalist.size());
    }

    public void setMaxItem(int maxNum) {
        this.maxNum = maxNum;
    }


}
